class Flag < ActiveRecord::Base

  attr_accessible :sneak, :flaggeable, :flaggeable_id, :flaggeable_type, :report_type

  ########## Associations
  belongs_to :sneak, :polymorphic => true
  belongs_to :flaggeable, :polymorphic => true

  ########## Validations
  validates_presence_of :sneak_type, :sneak_id, :flaggeable_id, :flaggeable_type, :report_type

  validates_inclusion_of :report_type, :in => CLAIM_TYPES || ["spam", "offensive", "attention"]

  validate :should_be_unique

  protected

  def should_be_unique
    flag = Flag.first(:conditions => ['flaggeable_type = ? and flaggeable_id = ? and sneak_type = ? and sneak_id = ?', self.flaggeable_type, self.flaggeable_id, self.sneak_type, self.sneak_id])
    
    self.errors.add(:flagged, I18n.t(:"flags.messages.already_flagged")) unless flag.nil? || flag.id == self.id
  end

end
